package com.github.editor;

import com.github.editor.util.TextUtils;

public class Main {
    public static final String UMENG_CHANNEL = "UMENG_CHANNEL";

    private static void showHelp() {
        //修改渠道号
        //修改应用名
        //修改应用图标
        System.out.println("-f [ApkPath] -n [AppName] -i [IconUrl] -u [Channel] -o [OutApkPath] -t [tempPath]");
        System.out.println("-file [ApkPath] -name [AppName] -icon [IconUrl] -umeng [Channel] -output [OutApkPath] -temp [tempPath]");
        System.out.println("-f D:\\a.apk -n \"App Name\" -i D:\\1.png -u k01 -o D:\\b.apk -t D:\\temp");
    }

    public static void main(String[] args) {
        String apkPath = null;
        String newAppName = null;
        String newIconUrl = null;
        String outPath = "new.apk";
        String newChannel = null;
        String tempPath = "temp";
        String get_arg = null;
        boolean debug = false;
        try {
            for (int i = 0; i < args.length; i++) {
                String k = args[i];
                if ("-f".equals(k) || "-file".equals(k)) {
                    apkPath = args[++i];
                } else if ("-n".equals(k) || "-name".equals(k)) {
                    newAppName = args[++i];
                } else if ("-i".equals(k) || "-icon".equals(k)) {
                    newIconUrl = args[++i];
                } else if ("-u".equals(k) || "-umeng".equals(k)) {
                    newChannel = args[++i];
                } else if ("-o".equals(k) | "-output".equals(k)) {
                    outPath = args[++i];
                } else if ("-t".equals(k) || "-temp".equals(k)) {
                    tempPath = args[++i];
                } else if("-h".equals(k) || "/?".equals(k) || "--help".equals(k)){
                    showHelp();
                    return;
                } else if ("-v".equals(k)) {
                    debug = true;
                } else if("-get".equals(k)){
                    get_arg = args[++i];
                }
            }
        } catch (Throwable e) {
            showHelp();
            return;
        }
        if (TextUtils.isEmpty(apkPath)) {
            showHelp();
            return;
        }
        if(debug) {
            System.out.println("workPath=" + tempPath);
        }
        if (get_arg != null) {
            if ("channel".equals(get_arg)) {
                try {
                    System.out.println(ApkEditor.getMetadata(apkPath, UMENG_CHANNEL));
                } catch (Throwable e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
                return;
            }
        }
        try(ApkEditor apkEditor = new ApkEditor(apkPath, tempPath)) {
            apkEditor.setDebug(debug);
            if (!apkEditor.parse()) {
                return;
            }
            apkEditor.changeMetaData(UMENG_CHANNEL, newChannel);
            apkEditor.changeAppTitle(newAppName);
            apkEditor.changeAppIcon(newIconUrl);
            apkEditor.save(outPath);
            if(debug) {
                System.out.println("Apk's changes is completed");
            }
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

}
